import pymysql

from coding.DoFile import doLog

logger = doLog.getLogger(__name__)
class DoDb:
# 创建连接
    def __init__(self):
        try:
            db = pymysql.Connection(user="root",password="123456",host="172.16.0.254",port=3306,database="crm9")
            # 获取执行sql语句的对象
            self.r = db.cursor()
        except Exception as e:
            print("数据库连接失败")
            logger.error("数据库连接失败")
        else:
            logger.info("数据库连接成功")
        finally:
            logger.info("数据库连接结束")



    def dbSQL(self,sql):
        try:
            self.r.execute(sql)
        except Exception as e:
            logger.error(f"执行失败{sql},信息{e}")
        else:
            # 获取数据
            # 获取所有:fetchall
            # 获取一个:fetchone
            # #获取部分:fetchmany
            s1 = self.r.fetchall()
            print("执行成功")
            print(len(s1),type(s1),s1,sep="\n")
            return s1
        finally:
            self.r.close()
            logger.info("关闭成功")


# if __name__ == '__main__':
#     db = DoDb()
#     # sq = "select log_id from 72crm_oa_log"
#     # t = db.dbSQL(sq)
#     # for i in t:
#     #     print(i[0])
#     a = db.getSql()
#     print(a)


